package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.ClazzRequestDTO;
import com.tianya.domain.entity.Clazz;
import com.tianya.domain.entity.Teacher;
import com.tianya.domain.entity.User;
import com.tianya.domain.vo.ClazzVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.service.ClazzService;
import com.tianya.mapper.ClazzMapper;
import com.tianya.service.TeacherService;
import com.tianya.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【clazz(班级表)】的数据库操作Service实现
* @createDate 2025-05-24 18:59:04
*/
@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz>
    implements ClazzService{

    @Resource
    private TeacherService teacherService;

    @Resource
    private UserService userService;
    @Override
    public void createClazz(ClazzRequestDTO clazzRequestDTO) {
        Clazz clazz = new Clazz();
        clazz.setName(clazzRequestDTO.getName());
        clazz.setHeadTeacherId(clazzRequestDTO.getHeadTeacherId());
        clazz.setStudentCount(0L);
        this.save(clazz);
    }

    @Override
    public PageResultVO<ClazzVO> pageClazz(Integer pageNum, Integer pageSize, ClazzRequestDTO clazzRequestDTO) {
        Page<Clazz> pageParam = new Page<>(pageNum, pageSize);
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<Clazz>();
        //费控判断
        queryWrapper.like(clazzRequestDTO.getName() != null, "name", clazzRequestDTO.getName());
        Page<Clazz> pageResult = baseMapper.selectPage(pageParam, queryWrapper);
        PageResultVO<ClazzVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(pageResult.getTotal());
        List<ClazzVO> clazzVOList = new ArrayList<>();
        for (Clazz clazz : pageResult.getRecords()) {
            Long headTeacherId = clazz.getHeadTeacherId();
            Teacher teacher = teacherService.getById(headTeacherId);
            Long userId = teacher!=null?teacher.getUserId():null;
            User user = userService.getById(userId);
            ClazzVO clazzVO = new ClazzVO();
            clazzVO.setHeadTeacherName(user!=null?user.getName():"unKnown");
            BeanUtil.copyProperties(clazz, clazzVO);
            clazzVOList.add(clazzVO);
        }
        pageVO.setRecords(clazzVOList);
        return pageVO;
    }

    @Override
    public List<ClazzVO> listClazz() {
        return list(new QueryWrapper<Clazz>())
                .stream()
                .map(clazz -> {
                    ClazzVO clazzVO = new ClazzVO();
                    BeanUtil.copyProperties(clazz, clazzVO);
                    return clazzVO;
                })
                .toList();
    }
}




